package com.orange.controller;

import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.orange.handler.AuthSysValueService;
import com.orange.modules.entity.AuthSysValue;
import com.orange.util.ReturnCode;

@Controller
@RequestMapping("/sysValue")
public class AuthSysValueController extends BaseController{

	private static final Logger log = Logger.getLogger(AuthSysValueController.class);
	
	
	@Autowired
	private AuthSysValueService authSysValueService;
	
	@RequestMapping(value = "/updateSysValue" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> updateSysValue(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		try {
			String type = map.get("type");
			String value = map.get("keyValue");
			if(type==null || StringUtils.isBlank(type))
				return toResponse(null,ReturnCode.FAIL,"类型不能为空");
			if(value==null || StringUtils.isBlank(value))
				return toResponse(null,ReturnCode.FAIL,"参数值不能为空");
			AuthSysValue asv = authSysValueService.getSysValue(type);
			if(asv==null)
				return toResponse(null,ReturnCode.FAIL,"不存在该数据");
			asv.setSysvalue(value);
			authSysValueService.updateSysValue(asv);
			return toResponse(null,ReturnCode.SUCCESS,"修改成功");
		} catch (Exception e) {
			// TODO: handle exception
			log.error(e);
			return toResponse(null,ReturnCode.FAIL,"修改失败");
		}
		
	}
	
	/**
	 * 查询当前类型的数据状态
	 * @param request
	 * @param map
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(value = "/querySysValue" ,produces = MediaType.APPLICATION_JSON_VALUE+";charset=UTF-8", method=RequestMethod.POST)
	@ResponseBody
	public ResponseEntity<String> querySysValue(HttpServletRequest request,@RequestBody Map<String ,String> map) throws SQLException{
		String type = map.get("type");
		if(type==null || StringUtils.isBlank(type))
			return toResponse(null,ReturnCode.FAIL,"类型不能为空");
		AuthSysValue asv = authSysValueService.getSysValue(type);
		if(asv==null)
			return toResponse(null,ReturnCode.FAIL,"不存在该数据");
		return toResponse(asv,ReturnCode.SUCCESS,"查询成功");
	}
	
}
